package com.whcoding.encrypt.common;

import com.whcoding.encrypt.annotation.Decrypt;
import com.whcoding.encrypt.util.AESUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdviceAdapter;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;

/**
 * @program: spring-boot-learning
 * @description: 解密操作
 * @author: whcoding
 * @create: 2022-06-16 11:39
 **/
@EnableConfigurationProperties(EncryptProperties.class)
@ControllerAdvice
public class DecryptRequest extends RequestBodyAdviceAdapter {
	@Autowired
	EncryptProperties encryptProperties;

	@Override
	public boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
		return methodParameter.hasMethodAnnotation(Decrypt.class) || methodParameter.hasParameterAnnotation(Decrypt.class);
	}

	@Override
	public HttpInputMessage beforeBodyRead(final HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
		byte[] body = new byte[inputMessage.getBody().available()];
		inputMessage.getBody().read(body);
		try {
			byte[] decrypt = AESUtils.decrypt(body, encryptProperties.getKey().getBytes());
			final ByteArrayInputStream bais = new ByteArrayInputStream(decrypt);
			return new HttpInputMessage() {
				@Override
				public InputStream getBody() {
					return bais;
				}

				@Override
				public HttpHeaders getHeaders() {
					return inputMessage.getHeaders();
				}
			};
		} catch (Exception e) {
			e.printStackTrace();
		}
		return super.beforeBodyRead(inputMessage, parameter, targetType, converterType);
	}
}